import { loginAPI, loginTestAPI } from "@/api/profile";
import { defineStore } from "pinia";
import { ref } from "vue";

export const useUserStore = defineStore('user', () => {
  const userInfo = ref({})

  const onLogin = async (e) => {
    const { iv, encryptedData } = e.detail;
    const { code } = await uni.login();
    const res = await loginAPI({ iv, encryptedData, code });
    userInfo.value = res.result
    uni.showToast({ title: "登录成功" });
    setTimeout(() => {
      uni.navigateBack();
    }, 1500);
  };

  const onTestLogin = async () => {
    const res = await loginTestAPI("13577778888");
    userInfo.value = res.result
    uni.showToast({ title: "登录成功" });
    setTimeout(() => {
      uni.navigateBack();
    }, 1500);
  };

  return { userInfo, onLogin, onTestLogin }

},
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        }
      }
    }
  }
)


